package com.example.springioc2;

import com.example.springioc2.controller.UserController;
import com.example.springioc2.service.UserService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

//@SpringBootApplication
//public class SpringIoC2Application {
//
//    public static void main(String[] args) {
//        //获取Spring的上下文
//        ApplicationContext context = SpringApplication.run(SpringIoC2Application.class, args);
//        //从Spring的上下文中获取对象
//        //根据bean类型，从Spring上下文中获取对象
//        UserController userController1 = (UserController)context.getBean(UserController.class);
//        //根据bean名称，从Spring上下文获取对象
//        UserController userController2 = (UserController)context.getBean("userController");
//        //根据bean名称+类型，从Spring上下文获取对象
//        UserController userController3 = (UserController) context.getBean("userController", UserController.class);
//
//        System.out.println(userController1);
//        System.out.println(userController2);
//        System.out.println(userController3);
//    }
//}

////@ComponentScan({"com.example.springioc2.service"})
//@SpringBootApplication
//public class SpringIoC2Application {
//    public static void main(String[] args) {
//        //获取Spring上下文对象
//        ApplicationContext context = SpringApplication.run(SpringIoC2Application.class, args);
//        //从Spring中获取UserService对象
//        UserService userService = context.getBean(UserService.class);
//        //使用对象
//        userService.doService();
//    }
//}

//@SpringBootApplication
//public class SpringIoC2Application {
//    public static void main(String[] args) {
//        //获取Spring上下文对象
//        ApplicationContext context = SpringApplication.run(SpringIoC2Application.class, args);
//        //从Spring中获取UserRepository对象
//        UserRepository userRepository = context.getBean(UserRepository.class);
//        //使用对象
//        userRepository.doRepository();
//    }
//}

//@SpringBootApplication
//public class SpringIoC2Application {
//    public static void main(String[] args) {
//        //获取Spring上下文对象
//        ApplicationContext context = SpringApplication.run(SpringIoC2Application.class, args);
//        //从Spring中获取UserComponent对象
//        UserComponent userComponent = context.getBean(UserComponent.class);
//        //使用对象
//        userComponent.doComponent();
//    }
//}

//@SpringBootApplication
//public class SpringIoC2Application {
//    public static void main(String[] args) {
//        //获取Spring上下文对象
//        ApplicationContext context = SpringApplication.run(SpringIoC2Application.class, args);
//        //从Spring中获取UserComponent对象
//        UserConfiguration userConfiguration = context.getBean(UserConfiguration.class);
//        //使用对象
//        userConfiguration.doConfiguration();
//    }
//}

//@SpringBootApplication
//public class SpringIoC2Application {
//    public static void main(String[] args) {
//        //获取Spring上下文对象
//        ApplicationContext context = SpringApplication.run(SpringIoC2Application.class, args);
//        //从Spring中获取对象
//        UserInfo userInfo1 = (UserInfo) context.getBean("userInfo1");
//        UserInfo userInfo2 = (UserInfo) context.getBean("userInfo2");
//        //使用对象
//        System.out.println(userInfo1);
//        System.out.println(userInfo2);
//    }
//}

//@SpringBootApplication
//public class SpringIoC2Application {
//    public static void main(String[] args) {
//        //获取Spring上下文对象
//        ApplicationContext context = SpringApplication.run(SpringIoC2Application.class, args);
//        //从Spring上下文中获取对象
//        UserInfo u1 = (UserInfo)context.getBean("userInfo1");
//        //使用对象
//        System.out.println(u1);
//    }
//}

@SpringBootApplication
public class SpringIoC2Application {
    public static void main(String[] args) {
        //获取Spring上下文对象
        ApplicationContext context = SpringApplication.run(SpringIoC2Application.class, args);
        //从Spring上下文中获取对象
        UserController userController = (UserController) context.getBean("userController");
        //使用对象
        userController.sayHi();
    }
}